<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";
                
 require "../../files/tool_files_dirs.php";
                
 require "../../misc/class_tag.php";
 require "../../misc/tool_color.php";
                
 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";
                
 require "../../array/tool_array.php";
                
 require "../../pear_graph/tool_pear_graph.php";
                
 require "../../maths/tool_maths.php";
                
 require "../../string/tool_string.php";
 require "../../string/class_segment.php";
 require "../../string/class_suffixarray_new.php";
 require "../../string/suffix_array.php";
                
 require "../../pdo/class_sql_document.php";

//$dir="../../xml/europa_complete_repair/";
$lg_candidates=array("de","el","es","fi","fr","pl");

//$lg_candidates=array("et","fi","fr","hu","it","lt","lv","nl","pl","pt","sl","sw");
//$lg_candidates=array("cs","da","de");
//$array_ids_europa=tool_corpus::get_unified_europa($dir,$lg_candidates);
//$motif_lg="fr";

$exp=new sql_experience("align_ecr_selected");
$array_obj_sql=$exp->reader_experience();

$array_data=array();
$array_exists=array();
foreach($lg_candidates as $lg){
  $start=microtime(true);
  $all_bloc=$array_obj_sql['sql_bloc']->reader_lg2array($lg);
  $factory_rstr=new suffixarray();
  foreach($all_bloc as $ligne){
    $factory_rstr->__add_str($ligne['contenu_text'],$ligne['id_bloc']);
  }

  $all_res=$factory_rstr->__init();
  $count=count($all_res);
  $end=microtime(true);
  $total = $end-$start;
  echo "\ntotal_init $lg::$total\n";

  $start=microtime(true);
  $array_request=array();
  foreach($all_res as $c=>$rstr){
    echo "\r $c / $count";
    $su = $factory_rstr->__get_suffix($rstr[1][0]);
    $global_str = $factory_rstr->__get_str($su[2]);
    $str = mb_substr($global_str,$su[0],$rstr[0]);
/*
    if(!isset($array_exists[$str])){
      $exists=$array_obj_sql['sql_graphie']->reader_graphie2id_graphie($str);
      $array_exists[$str]=$exists[0];
    }
    $id_graphie=$array_exists[$str];
*/
    $exists = $array_obj_sql['sql_graphie']->reader_graphie2id_graphie($str);
    if(isset($exists[0][0])){
      $id_graphie = $exists[0][0];
    }
    else{
      $array_gr=array('graphie'=>"$str",'len'=>$rstr[0]);
      $id_graphie = $array_obj_sql['sql_graphie']->writer_values($array_gr);
    }

    foreach($rstr[1] as $pos){
      $su=$factory_rstr->__get_suffix($pos);
      $array_request[]=array(
        "id_bloc"=>$su[2],
        "id_graphie"=>$id_graphie,
        "offset"=>$su[0]
      );
      //$array_obj_sql['sql_rstr']->writer_values($array_request);
    }

//    $array_request=array();
  }

  $array_obj_sql['sql_rstr']->writer_array_values($array_request);
  unset($array_request);
  $end=microtime(true);
  $total = $end-$start;
  echo "\ntotal_calcul $lg::$total\n";
}

die();

?>
